﻿using System;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;
using System.Web.Security;

namespace NopSolutions.NopCommerce.Web.Administration
{
    public partial class LoginPage : BaseNopAdministrationPage
    {
        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            Load += new EventHandler(LoginPage_Load);
            LoginButton.Click += new EventHandler(LoginButton_Click);
        }



        void LoginPage_Load(object sender, EventArgs e) {
            //this.ApplyLocalization();

            CommonHelper.SetResponseNoCache(Response);

            string title = GetLocaleResourceString("PageTitle.Login");
            SEOHelper.RenderTitle(this, title, true);
        }

        void LoginButton_Click(object sender, EventArgs e) {
            Page.Validate("LoginForm");
            if (!Page.IsValid) {
                pnlError.Visible = true;
                FailureText.Text = GetLocaleResourceString("Login.FailureText");
            }
            else {
                if (Membership.ValidateUser(UserName.Text.Trim(), Password.Text.Trim())) {
                    FormsAuthentication.SetAuthCookie(UserName.Text.Trim(), RememberMe.Checked);
                    Response.Redirect("~/administration/default.aspx");
                }
                else {
                    pnlError.Visible = true;
                    FailureText.Text = GetLocaleResourceString("Login.FailureText");
                }
            }
        }

        private void ApplyLocalization() {
            Literal lUsernameOrEmail = FindControl("lUsernameOrEmail") as Literal;
            if (lUsernameOrEmail != null) {
                if (this.CustomerService.UsernamesEnabled) {
                    lUsernameOrEmail.Text = GetLocaleResourceString("Login.Username");
                }
                else {
                    lUsernameOrEmail.Text = GetLocaleResourceString("Login.E-MailAddress");
                }
            }
            RequiredFieldValidator UserNameOrEmailRequired = FindControl("UserNameOrEmailRequired") as RequiredFieldValidator;
            if (UserNameOrEmailRequired != null) {
                if (this.CustomerService.UsernamesEnabled) {
                    UserNameOrEmailRequired.ErrorMessage = GetLocaleResourceString("Login.UserNameRequired");
                    UserNameOrEmailRequired.ToolTip = GetLocaleResourceString("Login.UserNameRequired");
                }
                else {
                    UserNameOrEmailRequired.ErrorMessage = GetLocaleResourceString("Login.E-MailRequired");
                    UserNameOrEmailRequired.ToolTip = GetLocaleResourceString("Login.E-MailRequired");
                }
            }
        }

        protected override bool AdministratorSecurityValidationEnabled {
            get {
                return false;
            }
        }

        protected override bool IPAddressValidationEnabled {
            get {
                return false;
            }
        }
    }
}
